package com.supermap.dataservice.alltype;

import com.alibaba.dubbo.config.annotation.Service;

import com.supermap.dataservice.alltype.dao.AllTypeDao;
import com.supermap.dataservice.util.HttpRequest;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.alltype.AllTypeService;
import com.supermap.dubbo.api.alltype.entity.AllType;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxs
 * @Title:当天各类(咨询,农情,灾情,产品,农田)数据的实时 业务逻辑层
 * @Description:
 * @date 2018/8/2  11:51
 */
@Service
public class AllTypeServiceImpl implements AllTypeService {
    /**
     *使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AllTypeServiceImpl.class);
    @Resource
    private AllTypeDao allTypeDao;
    @Override
    public JSONUtil getAllTypeList(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        Map<String,List<AllType>> m=new HashMap();
        try {
            List<AllType> advList = allTypeDao.getAllTypeToAdvList(areaCode);
            m.put("咨询", advList);
            List<AllType> agrList = allTypeDao.getAllTypeToAgrList(areaCode);
            m.put("农情", agrList);
            List<AllType> disList = allTypeDao.getAllTypeToDisList(areaCode);
            m.put("灾情", disList);
            List<AllType> areaList = allTypeDao.getAreaByCode(areaCode);
            m.put("geo", areaList);
//            String result = HttpRequest.httpURLConectionGET("http://118.25.235.213:8080/sms/tBPmsProductController.do?productNum&areaId=" + areaCode);
//            //字符串String获得JSONArray对象
//            JSONArray jsonArray=JSONArray.fromObject(result);
//            //字符串JSONArray获得list对象
//            List<AllType> cpList = JSONArray.toList(jsonArray, new AllType(), new JsonConfig());
//            List<AllType> areaList = allTypeDao.getAreaByCode(areaCode);
//            m.put("产品", cpList);
            jsonUtil.setMsg("Get getAllTypeList success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(m);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AllTypeServiceImpl=>getAllTypeList=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getAllTypeList flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public JSONUtil getFarmCropNumByAreaCode(String areaCode) {
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            if (areaCode != null) {
                areaCode = areaCode + "%";
            }
            List<AllType> listFarm = allTypeDao.getFarmCropNumByAreaCode(areaCode);
            jsonUtil.setMsg("Get getFarmCropByAreaCode success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(listFarm);
        }catch (Exception e){
            //捕捉数据库异常
            logger.error("AllTypeServiceImpl=>getFarmCropByAreaCode=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("Get getFarmCropByAreaCode flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

}
